//
//  ST_BTAdvertViewController.swift
//  btvideo
//
//  Created by mac on 2020/5/11.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyAttributes
import SnapKit
import RealmSwift
import SwiftyUserDefaults
import SDWebImage
import RxSwift

class ST_BTAdvertViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = .white
        
        
        view.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(maskView)
        maskView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        view.addSubview(advert_logo_image_view)
        advert_logo_image_view.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 208, height: 83))
            make.top.equalToSuperview().offset(175-44+statusBarHeight)
            make.centerX.equalToSuperview()
        }
        
        view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(iPhoneXSeries ? -40 : -30)
        }
        
        
        view.addSubview(next_btn)
        next_btn.snp.makeConstraints { (make) in
            make.width.equalTo(60)
            make.height.equalTo(25)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(statusBarHeight+10)
        }
        
        self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timer_handle), userInfo: nil, repeats: true)
        
        bind_data()
        
    }
    
    
    fileprivate func bind_data(){
        
        guard let model = outside_model else { return  }
        
        let peculiarity = model.feature_name
        
        let nickName = model.nickname
        
        let str = "来自特质".withAttributes(self.attr0)
        
        str.append(peculiarity.withAttributes(self.attr1))
        
        str.append(nickName.withAttributes(self.attr0))
        
        self.content_label.attributedText = str
        
        if let imgKey = SDWebImageManager.shared.cacheKey(for: model.content.advert_image_url_str().url),let img = SDImageCache.shared.imageFromCache(forKey: imgKey){
            
            self.cover_image_view.image = img
            print("广告图片 缓存加载")
        }else{
            print("广告图片 缓存SdwebImage加载")
            self.cover_image_view.sd_setImage(with: model.content.advert_image_url_str().url, completed: nil)
        }
        
    }
    
    //    MARK: - Properties
    var outside_model:ST_AdvertModel?
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.backgroundColor = .black
        return control
    }()
    
    fileprivate lazy var maskView:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
        return control
    }()
    
    fileprivate lazy var advert_logo_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("advert_logo")
        return control
    }()
    
    let attr0: [Attribute] = [Attribute.font(UIFont.systemFont(ofSize: 12)),Attribute.textColor(UIColor(red: 1, green: 1, blue: 1, alpha: 1))]
    
    let attr1: [Attribute] = [Attribute.font(UIFont.systemFont(ofSize: 12)),Attribute.textColor(UIColor(red: 1, green: 0.76, blue: 0.03,alpha:1))]
    
    fileprivate lazy var next_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(white: 0, alpha: 0.2)
        control.cornerRadius = 12
        control.setTitleForAllStates("跳过(3)")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.setTitleColorForAllStates(UIColor.init(white: 1, alpha: 0.6))
        control.addTarget(self, action: #selector(next_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        return control
    }()

    fileprivate var curr_number = 3
    fileprivate var timer:Timer?
}


extension ST_BTAdvertViewController{
    
    @objc fileprivate func timer_handle(){
        
        curr_number -= 1
        
        if curr_number == 2{
            
            UIView.animate(withDuration: 3, animations: {
                self.cover_image_view.transform = CGAffineTransform.init(scaleX: 1.2, y: 1.2)
            }) { (f) in
                
            }
        }
        
        if curr_number == 0{
            
            next_btn.setTitleForAllStates("跳过(0)")
            
            timer?.invalidate()
            timer = nil
            
            UIView.animate(withDuration: 1.5, delay: 1, options: UIView.AnimationOptions.init(), animations: {
                
                self.view.alpha = 0.0
                
            }) { (f) in
                self.view.removeFromSuperview()
                self.removeFromParent()
                ST_BTLaunchController.shared.advertStatus.onNext(.finished)
            }
            
        }else{
            next_btn.setTitleForAllStates("跳过("+curr_number.string+")")
        }
    }
    
    @objc fileprivate func next_handle(){
        
        timer?.invalidate()
        timer = nil
        
        UIView.animate(withDuration: 0.75, delay: 0, options: UIView.AnimationOptions.init(), animations: {
            
            self.view.alpha = 0.0
            
        }) { (f) in
            self.view.removeFromSuperview()
            self.removeFromParent()
            
            ST_BTLaunchController.shared.advertStatus.onNext(.finished)
            
        }
        
    }
    
}






